package com.qf.Controller;



import com.qf.Entity.Category;
import com.qf.Entity.Page;
import com.qf.Entity.Product;
import com.qf.Service.CategoryService;
import com.qf.Service.Impl.CategoryServiceImpl;
import com.qf.Service.Impl.ProductServiceImpl;
import com.qf.Service.ProductService;
import com.qf.Util.Constants;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/*2021/10/9*/
@WebServlet( "/product")
public class ProductController extends BaseController {
    public String  selectPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //1.从页面中收取pageIndex参数
        String pageIndex = request.getParameter("pageIndex");
        if(pageIndex==null){
            pageIndex="1";
        }
        Page page=new Page(Integer.valueOf(pageIndex));
        //2.调用业务进行分页信息查询
        ProductService productService=new ProductServiceImpl();
        List<Product> products = productService.selectPage(page);
        CategoryService categoryService=new CategoryServiceImpl();
        List<Category> categories = categoryService.queryAllCategory();
        //3.将结果存作用域然后进行转发
        request.setAttribute("products",products);
        request.getSession().setAttribute("page", page);
        request.getSession().setAttribute("categories", categories);
        return Constants.FORWARD+"/list.jsp";
    }
    public String modifyProduct(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //1.收参
        String pId = request.getParameter("pId");
        String pName = request.getParameter("pName");
        String pDate1 = request.getParameter("pDate");
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        Date pDate =null;
        try {
            pDate = simpleDateFormat.parse(pDate1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String pVender = request.getParameter("pVender");
        String pPrice = request.getParameter("pPrice");
        String pType = request.getParameter("pType");
        String cId = request.getParameter("cId");

        //2.判断走的增加还是修改业务
        ProductService productService=new ProductServiceImpl();
        Page page =(Page) request.getSession().getAttribute("page");

        if(pId!=""){//走的是修改业务
            Product product=new Product(Integer.valueOf(pId), pName, pDate, pVender, Double.valueOf(pPrice), Integer.valueOf(cId), Integer.valueOf(pType));
            Integer result = productService.updateProduct(product);
            if(result>0){
                return Constants.REDIRECT+"/product?method=selectPage&pageIndex="+Integer.valueOf(pId)/page.getPageSize();
            }

        }//走的是增加业务
        else {
            Product product=new Product(0, pName, pDate, pVender, Double.valueOf(pPrice), Integer.valueOf(cId), Integer.valueOf(pType));
            System.out.println(product);
            Integer result = productService.insertProduct(product);

                return Constants.REDIRECT+"/product?method=selectPage&pageIndex="+page.getTotalPage();

        }
        return Constants.REDIRECT+"/product?method=selectOneProduct&pId="+Integer.valueOf(pId); //修改失败，返回当前修改页面
    }
    public String selectOneProduct(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.收参
        String pId = request.getParameter("pId");
        ProductService productService=new ProductServiceImpl();
        Product product = productService.selectBypId(Integer.valueOf(pId));
        request.setAttribute("product", product);
        return Constants.FORWARD+"/edit.jsp";
    }
    public String deleteOneProduct(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.收参
        String pId = request.getParameter("pId");
        //2.调用业务进行删除
        ProductService productService=new ProductServiceImpl();
        Integer integer = productService.deleteProduct(Integer.valueOf(pId));
        //3.重定向到当前页面
        if(integer>0){
            return "1";
        }
        return "0";
    }

}
